package com.customview.wzq.zrwidgetdemo;

import android.app.Activity;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.RadioGroup;
import android.widget.RemoteViews;
import android.widget.Toast;

/**
 * 创建时间：2017/7/5
 * 作者：kb_jay
 * 功能描述：
 */

public class ListWidgetConfigureActivity extends Activity implements View.OnClickListener {

    private int mWidgetid;
    private RadioGroup mModeGroup;
    private Button mBtAddWidget;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_widget_config);
        mModeGroup = (RadioGroup) this.findViewById(R.id.rg_configure_mode);
        mBtAddWidget = (Button) this.findViewById(R.id.btn_configure);
        mBtAddWidget.setOnClickListener(this);

        mWidgetid = getIntent().getIntExtra(
                AppWidgetManager.EXTRA_APPWIDGET_ID,
                AppWidgetManager.INVALID_APPWIDGET_ID);
        setResult(RESULT_CANCELED);
    }

    @Override
    public void onClick(View v) {
        if (R.id.btn_configure == v.getId()) {
            SharedPreferences sp = getSharedPreferences(
                    String.valueOf(mWidgetid), MODE_PRIVATE);
            SharedPreferences.Editor editor = sp.edit();

            RemoteViews rv = new RemoteViews(getPackageName(), R.layout.zr_widget);
            switch (mModeGroup.getCheckedRadioButtonId()) {
                case R.id.rb_mode_image:
                    editor.putString(ListWidgeService.KEY_MODE,
                            ListWidgeService.MODE_IMAGE);
                    editor.commit();
                    rv.setTextViewText(R.id.tv_list_widget_title, "Image Collection");
                    break;
                case R.id.rb_mode_media:
                    editor.putString(ListWidgeService.KEY_MODE,
                            ListWidgeService.MODE_MEDIA);
                    editor.commit();
                    rv.setTextViewText(R.id.tv_list_widget_title,"Media Collection");
                    break;
                default:
                    Toast.makeText(this, "please select a Media Type", Toast.LENGTH_LONG).show();
                    return;
            }

            Intent intent = new Intent(this, ListWidgeService.class);
            intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,mWidgetid);
            intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));// TODO: 2017/7/5

            rv.setRemoteAdapter(mWidgetid,R.id.list,intent);
            rv.setEmptyView(R.id.list,R.id.list_empty);

            Intent viewIntent = new Intent(Intent.ACTION_VIEW);
            PendingIntent pi = PendingIntent.getActivity(this, 0, viewIntent, 0);

            rv.setPendingIntentTemplate(R.id.list,pi);

            AppWidgetManager manager = AppWidgetManager.getInstance(this);
            manager.updateAppWidget(mWidgetid,rv);

            Intent data = new Intent();
            data.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,mWidgetid);
            setResult(RESULT_OK,data);
            finish();
        }
    }
}
